package observer

type subject interface {
	register(ob observer)
	remove(ob observer)
	notify(message string)
}

type simpleSubject struct {
	obs map[string]observer
}

func (s *simpleSubject) register(ob observer) {
	if _, ok := s.obs[ob.name()]; ok {
		return
	}
	s.obs[ob.name()] = ob
}

func (s *simpleSubject) remove(ob observer) {
	if _, ok := s.obs[ob.name()]; ok {
		s.obs[ob.name()] = nil
	}
}

func (s *simpleSubject) notify(message string) {
	go func() {
		for _, v := range s.obs {
			v.onListen(message)
		}
	}()
}
